package gov.tax.rti.base.security.dao.impl;

import gov.tax.rti.base.jdbc.BaseJdbcDaoSupport;
import gov.tax.rti.base.security.entity.Sjbq;
import gov.tax.rti.base.security.entity.Yhbs;
import gov.tax.rti.core.model.RtiSqlCommand;
import gov.tax.rti.core.security.dao.UserDao;
import gov.tax.rti.core.security.entity.DataTag;
import gov.tax.rti.core.security.entity.User;
import gov.tax.rti.core.system.ChainedUtils;
import gov.tax.rti.core.util.RtiConstants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository//("rti.core.userDao")
public class YhbsJdbcImpl extends BaseJdbcDaoSupport<Yhbs, String> implements UserDao {

	public User getUserByLoginName(String loginName) {
		String sql = "SELECT t.* FROM " + RtiConstants.SCHEMA+ ".RTI_YHBS t WHERE t.YHBS_BM = ? AND t.YXBZ > 0";
		try {
			return queryObj(sql, new Object[]{loginName});
			//return (User) queryObj(sql, new Object[]{loginName}, Yhbs.class);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	public User getUserById(String id) {
		String sql = "SELECT t.* FROM " + RtiConstants.SCHEMA+ ".RTI_YHBS t WHERE t.YHBS_DM = ? AND t.YXBZ > 0";
		try {
			return queryObj(sql, new Object[]{id});
			//return (User) queryObj(sql, new Object[]{loginName}, Yhbs.class);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	/*private class UserRowMapper implements RowMapper<User> {
		public User mapRow(final ResultSet rs, final int arg1) throws SQLException {
			Yhbs user = new Yhbs();
			user.setYhbsBm(rs.getString("YHBS_BM"));
			user.setYhbsDm(rs.getString("YHBS_DM"));
			user.setYhbsMc(rs.getString("YHBS_MC"));
			user.setYxbz(rs.getInt("YXBZ"));
			user.setQysj(rs.getDate("QYSJ"));
			user.setSxsj(rs.getDate("SXSJ"));
			user.setYhbsSy(rs.getString("YHBS_SY"));
			user.setYhbsSx(rs.getString("YHBS_SX"));
			user.setGxsz(rs.getString("GXSZ"));
			user.setGlbqDm(rs.getString("GLBQ_DM"));
			return user;
		}
	}*/

	public List<Map<String, Object>> getNSr(int dataSize,RtiSqlCommand rtiSqlCommand, String filterValue) {
		Object paras[] = null;
		int i = 0;
		StringBuffer sb = new StringBuffer(rtiSqlCommand.getCommandStr());
		sb.append(" SELECT X.YWDX_BM, X.YWDX_MC, X.YYXH FROM " + RtiConstants.SCHEMA + ".RTI_YWDX X, "+ChainedUtils.WITH_TABLE_NAME+" A");
		sb.append(" WHERE X.SJBQ_DM = A.SJBQ_DM ");
		if (StringUtils.isNotEmpty(filterValue)) {
			i = 2;
			paras = new Object[i + 1];
			sb.append(" AND (Upper(X.YWDX_BM) LIKE ? OR Upper(X.YWDX_MC) LIKE ? )");
			paras[0] = paras[1] = "%" + filterValue.toUpperCase() + "%";
		} else {
			paras = new Object[i + 1];
		}
		paras[i] = dataSize;
		sb.append(" AND ROWNUM <= ? ");
		sb.append(" ORDER BY X.YWDX_SY ");

		return this.getJdbcTemplate().query(sb.toString(), paras, new RowMapper<Map<String, Object>>(){
			public Map<String, Object> mapRow(final ResultSet rs, final int arg1) throws SQLException {
				Map<String, Object> nsr = new HashMap<String, Object>();
				nsr.put("NSRSBH", rs.getString("YWDX_BM"));
				nsr.put("NSRMC", rs.getString("YWDX_MC"));
				nsr.put("NSRDZDAH", rs.getBigDecimal("YYXH").toString());
				return nsr;
			}
		});
	}

	@Override
	public List<User> getUsetByDept(String deptId) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT DISTINCT C.*  ");
		sb.append(" FROM " + RtiConstants.SCHEMA+ ".RTI_YHGW_YHBS A ");
		sb.append(" INNER JOIN " + RtiConstants.SCHEMA+ ".RTI_SJBQ B ON(B.SJBQ_DM = A.SJBQ_DM) ");
		sb.append(" INNER JOIN " + RtiConstants.SCHEMA+ ".RTI_YHBS C ON(C.YHBS_DM = A.YHBS_DM) ");
		sb.append(" WHERE B.YXBZ > 0 AND C.YXBZ > 0 AND A.SJBQ_DM = ? ");
		return queryIObjs(sb.toString(), new Object[]{deptId}, User.class);
	}


}
